package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.enums.MarketingConstant;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.GoodsMessage;
import com.songlanyun.modules.message.model.PayMessage;
import com.songlanyun.modules.message.model.RightsMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import com.songlanyun.modules.rights.entity.RightsChildOrder;
import com.songlanyun.modules.rights.entity.RightsOrder;
import com.songlanyun.modules.rights.service.RightsOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 维权  --  售后消息
 */
@Service("rightsInteractService")
public class RightsInteractServiceImpl implements IInteractMsgService {

    @Resource
    private RightsOrderService rightsOrderService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {
        RightsOrder rightsOrder = rightsOrderService.getById(to.getModelId());
        if (rightsOrder == null) {
            return;
        }
        PrivateMessageConstant.RightsStatus rightsStatus;

        //判断维权状态
        switch (rightsOrder.getRightsOperateStatus()) {
            case AGREE:
                rightsStatus = PrivateMessageConstant.RightsStatus.AGREE;
                break;
            case REFUSE:
                rightsStatus = PrivateMessageConstant.RightsStatus.REFUSE;
                break;
            case SHOP_RECEIPT:
                rightsStatus = PrivateMessageConstant.RightsStatus.RECEIPT;
                break;
            case REFUND_COMPLETE:
                rightsStatus = PrivateMessageConstant.RightsStatus.REFUND;
                break;
            default:
                return;
        }

        //备注
        String remark = String.format("你提交的售后订单：%s,%s,点击查看详情", rightsOrder.getRightsCode(), rightsStatus.getMsg());

        List<GoodsMessage> goodsList = new ArrayList<>();
        //增加子订单中的商品
        List<RightsChildOrder> rightsChildOrders = rightsOrder.loadRightsChildOrders();

        //维权商品
        rightsChildOrders.forEach(c -> {
            GoodsMessage goodsMessage = new GoodsMessage();
            goodsMessage.setGoodsId(c.getGoodsId());
            goodsMessage.setGoodsName(c.getGoodsTitle());
            goodsMessage.setGoodsImg(c.getGoodsImg());
            goodsMessage.setGoodsSpec(c.getSkuSetName());
            goodsList.add(goodsMessage);
        });

        //保存
        RightsMessage rightsMessage = new RightsMessage(rightsStatus, rightsOrder.getRefundMoney(),
                rightsOrder.getRefundPoint(), rightsOrder.getOrderId(),rightsOrder.getId(), goodsList);

        PrivateMessageTO privateMessageTO = new PrivateMessageTO(rightsOrder.getUserId(),
                PrivateMessageConstant.MessageType.RIGHTS_MESSAGE, rightsMessage, remark);
        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);

        privateMessageService.save(privateMessage);

        //单推
        geTuiUtils.pushByUserId(
                Msg.builder()
                        .uid(rightsOrder.getUserId())
                        .title(PrivateMessageConstant.MessageType.RIGHTS_MESSAGE.getMsg())
                        .content(remark)
                        .msg(privateMessage.pushMessageBody())
                        .build()
        );


    }
}
